#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "stu.h"

int main() {
    LinkList L, newL;
    L = NULL;
    // L->next = NULL;
    newL = NULL;
    int op, len, i, loc;
    ElemType e, pre, next;
    char file[] = "data1.txt";
    help();
    printf("请输入您的选择（0到17）  \n");
    scanf("%d", &op);
    while (op) {
        switch (op) {
        case 1:
            i = InitList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表已存在\n");
                break;
            case ERROR:
                printf("线性表创建失败\n");
                break;
            case OK:
                printf("成功\n");
                inputLIst(L);
                break;
            }
            break;
        case 2:
            i = DestroyList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 3:
            i = ClearList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("成功\n");
                printf("是否要重新输入元素(1代表输入0代表不输入)\n");
                scanf("%d", &i);
                if (i)
                    inputLIst(L);
                break;
            }
            break;
        case 4:
            i = ListEmpty(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("此线性表为空\n");
                break;
            case FALSE:
                printf("此线性表不为空\n");
                break;
            }
            break;
        case 5:
            len = ListLength(L);
            switch (len) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            default:
                printf("此线性表长度为%d\n", len);
                break;
            }
            break;
        case 6:
            printf("请输入您要获取的第几个元素\n");
            scanf("%d", &loc);
            i = GetElem(L, loc, e);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("您输入的数据不合法\n");
                break;
            case OK:
                printf("第%d个元素是%d\n", loc, e);
                break;
            }
            break;
        case 7:
            printf("请输入您要查找的元素\n");
            scanf("%d", &e);
            i = LocateElem(L, e);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("此元素不存在\n");
                break;
            default:
                printf("您要查找的元素%d的位置是%d\n", e, i);
                break;
            }
            break;
        case 8:
            printf("请输入元素\n");
            scanf("%d", &e);
            i = PriorElem(L, e, pre);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("无此元素或者此元素无前驱\n");
                break;
            case OK:
                printf("元素%d的前驱是%d\n", e, pre);
                break;
            }
            break;
        case 9:
            printf("请输入元素\n");
            scanf("%d", &e);
            i = NextElem(L, e, next);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("无此元素或者此元素无后继\n");
                break;
            case OK:
                printf("元素%d的后继是%d\n", e, next);
                break;
            }
            break;
        case 10:
            printf("请依次输入插入的位置和元素\n");
            scanf("%d%d", &loc, &e);
            i = ListInsert(L, loc, e);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("您输入的位置不合法\n");
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 11:
            printf("请输入您要删除的元素位置\n");
            scanf("%d", &loc);
            i = ListDelete(L, loc, e);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("您输入的数据不合法\n");
                break;
            case OK:
                ListTraverse(L);
                printf("被删除的元素是%d", e);
                break;
            }
            break;
        case 12:
            i = ListTraverse(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 13:
            i = SaveList(L, file);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("文件打开失败\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 14:
            i = LoadList(L, file);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("文件打开失败\n");
                break;
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 15:
            i = reverseList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 16:
            printf("请输入倒数第n个结点\n");
            scanf("%d", &loc);
            i = RemoveNthFromEnd(L, loc);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("您输入的数据不合法\n");
                break;
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 17:
            i = sortList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 18:
            help();
            break;
        }
        printf("请输入你的选择（0-17）\n");
        scanf("%d", &op);
    }
    DestroyList(L);
    printf("谢谢您的使用\n");
    return 0;
}